﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ProfileManagement
{
    public partial class ListConsultant : Page
    {
        private ConsultantEntity _consultant;
        private int _roleId;
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();

            if (Session["RoleId"] != null )
            {
                _roleId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
                if (_roleId == 1)
                {
                    if(!IsPostBack)
                    {
                        BindData();                        
                    }
                }
                else if (_roleId == 4)
                {
                    Response.Redirect("ViewConsultant.aspx");
                }
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ButtonSearchClick(null, null);
            ListViewConsultant.DataBind();
        }

        private void BindData()
        {
            ListViewConsultant.DataSource = ConsultantProfileLogic.SelectData();
            ListViewConsultant.DataBind();
        }

        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int)FunctionList.ListConsultant;

            if (Page.Master != null)
            {
                var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");
                AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
            }
        }

        protected void ListViewConsultantItemCommand(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                var id = int.Parse(e.CommandArgument.ToString(),CultureInfo.InvariantCulture);
                _consultant = new ConsultantEntity { ConsultantId = id };
                ConsultantProfileLogic.DeleteConsultant(_consultant);
                BindData();
            }
        }

        /// <summary>
        /// Filter search clients
        /// </summary>
        protected void FilterConsultant()
        {
            var fullName = textFullName.Text;
            var companyName = textCompanyName.Text;

            var query = ConsultantProfileLogic.SearchConsultant(fullName, companyName);
            if(query==null)
            {
                return;
            }
            ListViewConsultant.DataSource = query;
            ListViewConsultant.DataBind();
        }
        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            FilterConsultant();
        }
        /* or we get "raised event ItemDeleting which wasn't handled" */
        protected void ListViewConsultantItemDeleting(object sender, ListViewDeleteEventArgs e)
        {}

        protected void LinkDelSelectedClick(object sender, EventArgs e)
        {
            foreach (var listViewDataItem in ListViewConsultant.Items)
            {
                var checkBox = listViewDataItem.FindControl("checkDelete") as HtmlInputCheckBox;

                if (checkBox != null && checkBox.Checked)
                {
                    var a = checkBox.Value;
                    var consultantEntity = new ConsultantEntity
                    {
                        ConsultantId = int.Parse((a), CultureInfo.InvariantCulture)
                    };
                    var result = ConsultantProfileLogic.DeleteConsultant(consultantEntity);
                    if (result != "Successfully!")
                    {
                        return;
                    }
                }
            }
            BindData();
        }
    }
}
